﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Ledict.Utility.Extension
{
    /// <summary>
    /// Lis扩展方法
    /// </summary>
    /// <remarks>
    /// Create Date:2013-01-01
    /// Create Author:Zhangpeng
    /// Version:1.0
    /// 
    /// Edit Date:2014-03-21
    /// Edit Author:Zhangpeng
    /// </remarks>
    public static class ListExension
    {
        #region Method

        #region 分隔符相关操作
        /// <summary>
        /// 带相同分隔符的字符串类型的数字转换成List
        /// </summary>
        /// <param name="list">带相同分隔符的字符串</param>
        /// <param name="spliter">分隔符</param>
        /// <returns></returns>
        public static List<int> StringToList(this string list, char spliter)
        {
            string[] strs = list.Split(spliter);
            var result = new List<int>();
            foreach (string str in strs)
            {
                int number;
                if (!str.Equals(""))
                {
                    if (int.TryParse(str, out number))
                    {
                        result.Add(number);
                    }
                    else
                    {
                        return new List<int>();
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// list转换成指定分隔符分隔的字符串
        /// </summary>
        /// <param name="list">list</param>
        /// <param name="spliter">分隔符</param>
        /// <returns></returns>
        public static string ListToString(this List<int> list, char spliter)
        {
            string str = list.Aggregate(string.Empty, (current, i) => current + (i + spliter));
            return str.Substring(0, str.Length - 1);
        }

        /// <summary>
        /// list转换成指定分隔符分隔的字符串(泛型)  add by 张鹏
        /// </summary>
        /// <param name="list">list</param>
        /// <param name="spliter">分隔符</param>
        /// <returns></returns>
        public static string ListToString<T>(this IEnumerable<T> list, string spliter)
        {
            spliter = spliter ?? string.Empty;
            var str = new StringBuilder();
            if (list != null && list.Any())
            {
                foreach (T item in list)
                {
                    str.Append(item);
                    str.Append(spliter);
                }
            }
            if (str.ToString().EndsWith(spliter))
            {
                return str.ToString().Substring(0, str.Length - 1);
            }
            return str.ToString();
            //var str = list.Aggregate(string.Empty, (current, i) => current + (i + ","));
            //return str.Substring(0, str.Length - 1);
        }

        /// <summary>
        /// list转换成指定分隔符分隔的字符串，字符串之间带符号(泛型)  
        /// </summary>
        /// <param name="list">字符串list</param>
        /// <param name="spliter">分隔符</param>
        /// <param name="sign">符号</param>
        /// <returns>字符串</returns>
        public static string ListToStringBySign<T>(this IEnumerable<T> list, string spliter, string sign)
        {
            spliter = spliter ?? string.Empty;
            var str = new StringBuilder();
            if (list != null && list.Any())
            {
                foreach (T item in list)
                {
                    str.Append(sign + item + sign);
                    str.Append(spliter);
                }
            }
            if (str.ToString().EndsWith(spliter))
            {
                return str.ToString().Substring(0, str.Length - 1);
            }
            return str.ToString();

        }
        #endregion

        #region list查询扩展
        /// <summary>
        /// IQueryable类型集合的扩展查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="source">原集合</param>
        /// <param name="predicate">查询表达式</param>
        /// <param name="condition">是否查询</param>
        /// <returns>结果</returns>
        public static IQueryable<T> WhereIf<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate,
                                               bool condition)
        {
            return condition ? source.Where(predicate) : source;
        }

        /// <summary>
        /// IQueryable类型集合的扩展查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="source">原集合</param>
        /// <param name="predicate">查询表达式</param>
        /// <param name="condition">是否查询</param>
        /// <returns>结果</returns>
        public static IQueryable<T> WhereIf<T>(this IQueryable<T> source, Expression<Func<T, int, bool>> predicate,
                                               bool condition)
        {
            return condition ? source.Where(predicate) : source;
        }



        #endregion

        #region Distinct扩展
        public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
        {
            return source.Distinct(new CommonEqualityComparer<T, V>(keySelector));
        }
        #endregion

        #endregion


    }

    #region 相等比较类
    /// <summary>
    /// 相等比较类
    /// </summary>
    /// <typeparam name="T">泛型实体</typeparam>
    /// <typeparam name="V">需要比较属性</typeparam>
    public class CommonEqualityComparer<T, V> : IEqualityComparer<T>
    {
        private Func<T, V> keySelector;

        public CommonEqualityComparer(Func<T, V> keySelector)
        {
            this.keySelector = keySelector;
        }

        public bool Equals(T x, T y)
        {
            return EqualityComparer<V>.Default.Equals(keySelector(x), keySelector(y));
        }

        public int GetHashCode(T obj)
        {
            return EqualityComparer<V>.Default.GetHashCode(keySelector(obj));
        }
    }
    #endregion

}
